package edu.chalmers.platypus.view.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import edu.chalmers.platypus.model.BatchImage;
import edu.chalmers.platypus.model.IFilter;
import edu.chalmers.platypus.util.ComBus;
import edu.chalmers.platypus.util.StateChanges;
import edu.chalmers.platypus.view.PlatypusGUI;

/**
 * Panel showing loader animation and information about the files being saved
 * and the filters being applied. Also allows user to cancel operation.
 */
public class ApplyViewPanel extends javax.swing.JPanel implements
	PropertyChangeListener {

    /** Constructor */
    public ApplyViewPanel() {
	initComponents();
	ComBus.subscribe(this);
    }

    /** Constructor */
    public ApplyViewPanel(PlatypusView parent) {
	this();
	this.parent = parent;
    }

    /** Sets current file name text in GUI */
    public void setFileName(String name) {
	savingFileLabel.setText(this.savingFileText + " " + name);
    }

    /** Sets the name of the filter being applied in GUI */
    public void setFilterName(String name) {
	applyingFilterLabel.setText(this.applyFilterText + " " + name);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

	loaderPanel = new javax.swing.JLabel();
	savingFileLabel = new javax.swing.JLabel();
	applyingFilterLabel = new javax.swing.JLabel();
	abortOperationButton = new javax.swing.JButton();

	setName("Form"); // NOI18N

	loaderPanel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
		.getInstance().getContext()
		.getResourceMap(ApplyViewPanel.class);
	loaderPanel.setIcon(resourceMap.getIcon("loaderPanel.icon")); // NOI18N
	loaderPanel.setText(resourceMap.getString("loaderPanel.text")); // NOI18N
	loaderPanel.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
	loaderPanel.setName("loaderPanel"); // NOI18N

	this.savingFileText = resourceMap.getString("savingFileLabel.text");
	savingFileLabel
		.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	savingFileLabel.setText(savingFileText);
	savingFileLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
	savingFileLabel.setName("savingFileLabel"); // NOI18N

	applyFilterText = resourceMap.getString("applyingFilterLabel.text");
	applyingFilterLabel
		.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	applyingFilterLabel.setText(applyFilterText);
	applyingFilterLabel
		.setVerticalAlignment(javax.swing.SwingConstants.TOP);
	applyingFilterLabel.setName("applyingFilterLabel"); // NOI18N
	applyingFilterLabel
		.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

	abortOperationButton.setText(resourceMap
		.getString("abortOperationButton.text")); // NOI18N
	abortOperationButton.setName("abortOperationButton"); // NOI18N
	abortOperationButton
		.addActionListener(new java.awt.event.ActionListener() {
		    public void actionPerformed(java.awt.event.ActionEvent evt) {
			abortOperationButtonActionPerformed(evt);
		    }
		});

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
	this.setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.TRAILING)
						.addComponent(
							savingFileLabel,
							javax.swing.GroupLayout.Alignment.LEADING,
							javax.swing.GroupLayout.DEFAULT_SIZE,
							364, Short.MAX_VALUE)
						.addComponent(
							loaderPanel,
							javax.swing.GroupLayout.Alignment.LEADING,
							javax.swing.GroupLayout.DEFAULT_SIZE,
							364, Short.MAX_VALUE))
				.addContainerGap())
		.addComponent(applyingFilterLabel,
			javax.swing.GroupLayout.DEFAULT_SIZE, 384,
			Short.MAX_VALUE)
		.addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup()
				.addContainerGap(160, Short.MAX_VALUE)
				.addComponent(abortOperationButton)
				.addContainerGap(159, Short.MAX_VALUE)));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(loaderPanel,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					135, Short.MAX_VALUE)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(savingFileLabel)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(applyingFilterLabel,
					javax.swing.GroupLayout.PREFERRED_SIZE,
					23,
					javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(abortOperationButton)
				.addContainerGap(52, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    /** Abort operation button clicked. Aborts the operation. */
    private void abortOperationButtonActionPerformed(
	    java.awt.event.ActionEvent evt) {// GEN-FIRST:event_abortOperationButtonActionPerformed
	PlatypusGUI.getInstance().abortSaveOperation();
    }// GEN-LAST:event_abortOperationButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abortOperationButton;
    private javax.swing.JLabel applyingFilterLabel;
    private javax.swing.JLabel loaderPanel;
    private javax.swing.JLabel savingFileLabel;
    // End of variables declaration//GEN-END:variables

    private PlatypusView parent;
    private String applyFilterText;
    private String savingFileText;

    /** Recieves info about backend operations */
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
	String change = evt.getPropertyName();
	if (change.equals(StateChanges.PROCESSING_IMAGE.toString())) {
	    // New image is being saved, sets text
	    BatchImage b = (BatchImage) evt.getNewValue();
	    setFileName(b.getFileName());
	} else if (change.equals(StateChanges.APPLYING_FILTER.toString())) {
	    // New filter is being applied on image, sets text
	    IFilter f = (IFilter) evt.getNewValue();
	    setFilterName(f.getName());
	} else if (change.equals(StateChanges.SAVE_OPERATION_FINISHED
		.toString())) {
	    // Save operation is finished, shows next view (FinishedViewPanel)
	    parent.showNextView();
	} else if (change
		.equals(StateChanges.SAVE_OPERATION_ABORTED.toString())) {
	    // Save operation was aborted, shows next view (FinishedViewPanel)
	    parent.showNextView();
	}
    }
}
